#!/usr/bin/env sh

######################################################################
# @author      : Xiaotian Wu (yetist@gmail.com)
# @file        : run
# @created     : 星期日 6月 02, 2024 14:28:03 CST
#
# @description :
######################################################################
set -ex

script_dir=$(dirname $(realpath $0))
rust_version=1.68.2

cd $script_dir
if [ ! -d $HOME/ohos-sdk ]; then
  ./scripts/ohos-sdk.sh
fi

# env
export CC_aarch64_linux_ohos=$script_dir/ohos/aarch64-unknown-linux-ohos-clang.sh
export AR_aarch64_linux_ohos=$HOME/ohos-sdk/native/llvm/bin/llvm-ar
export CXX_aarch64_linux_ohos=$script_dir/ohos/aarch64-unknown-linux-ohos-clang++.sh

export CC_aarch64_unknown_linux_ohos=$script_dir/ohos/aarch64-unknown-linux-ohos-clang.sh
export AR_aarch64_unknown_linux_ohos=$HOME/ohos-sdk/native/llvm/bin/llvm-ar
export CXX_aarch64_unknown_linux_ohos=$script_dir/ohos/aarch64-unknown-linux-ohos-clang++.sh

export CC_armv7_linux_ohos=$script_dir/ohos/armv7-unknown-linux-ohos-clang.sh
export AR_armv7_linux_ohos=$HOME/ohos-sdk/native/llvm/bin/llvm-ar
export CXX_armv7_linux_ohos=$script_dir/ohos/armv7-unknown-linux-ohos-clang++.sh

export CC_armv7_unknown_linux_ohos=$script_dir/ohos/armv7-unknown-linux-ohos-clang.sh
export AR_armv7_unknown_linux_ohos=$HOME/ohos-sdk/native/llvm/bin/llvm-ar
export CXX_armv7_unknown_linux_ohos=$script_dir/ohos/armv7-unknown-linux-ohos-clang++.sh

export CC_loongarch64_linux_ohos=$script_dir/ohos/loongarch64-unknown-linux-ohos-clang.sh
export AR_loongarch64_linux_ohos=$HOME/ohos-sdk/native/llvm/bin/llvm-ar
export CXX_loongarch64_linux_ohos=$script_dir/ohos/loongarch64-unknown-linux-ohos-clang++.sh

export CC_loongarch64_unknown_linux_ohos=$script_dir/ohos/loongarch64-unknown-linux-ohos-clang.sh
export AR_loongarch64_unknown_linux_ohos=$HOME/ohos-sdk/native/llvm/bin/llvm-ar
export CXX_loongarch64_unknown_linux_ohos=$script_dir/ohos/loongarch64-unknown-linux-ohos-clang++.sh

export CC_x86_64_unknown_linux_ohos=$script_dir/ohos/x86_64-unknown-linux-ohos-clang.sh
export AR_x86_64_unknown_linux_ohos=$HOME/ohos-sdk/native/llvm/bin/llvm-ar
export CXX_x86_64_unknown_linux_ohos=$script_dir/ohos/x86_64-unknown-linux-ohos-clang++.sh

# 准备 rust 源代码
if [ ! -d rustc-${rust_version}-src ];then
  git clone --depth=1 --single-branch --recurse-submodules -b la64/${rust_version}/v2 https://github.com/yetist/rust.git rustc-${rust_version}-src
fi

# patches (crates)
# libc: https://github.com/rust-lang/libc/commit/c90efd90013f97693d589d0fdd9157337e82dafb
# compiler-builtins: https://github.com/rust-lang/compiler-builtins/commit/8dc0e28e03bc408fb97200939e3e68c2e30d0081

# prebuild rust with ohos target support
export PATH=$script_dir/ohos:$HOME/ohos-sdk/native/llvm/bin:$HOME/rust-1.68.2/bin:$PATH
cd rustc-${rust_version}-src
./configure --enable-local-rust --prefix=/ --disable-docs --set llvm.download-ci-llvm=false --set rust.rpath=true --set build.extended=true --set rust.lld=true --set rust.use-lld=true

DESTDIR=$HOME/rust-1.68.2 python3 ./x.py dist

# install prebuild rust with ohos target support
mkdir -p $HOME/rust-1.68.2
for i in cargo-${rust_version}-dev-x86_64-unknown-linux-gnu rustc-${rust_version}-dev-x86_64-unknown-linux-gnu rust-std-${rust_version}-dev-x86_64-unknown-linux-gnu; do
  tar -C /tmp/ -xf build/dist/${i}.tar.gz
  /tmp/${i}/install.sh --prefix=$HOME/rust-1.68.2
done
mv build build.old
mv config.toml config.toml.old

# build  rust-std packages
TARGETS=aarch64-linux-ohos
TARGETS=$TARGETS,aarch64-unknown-linux-ohos
TARGETS=$TARGETS,armv7-linux-ohos
TARGETS=$TARGETS,armv7-unknown-linux-ohos
TARGETS=$TARGETS,loongarch64-linux-ohos
TARGETS=$TARGETS,loongarch64-unknown-linux-ohos
TARGETS=$TARGETS,x86_64-unknown-linux-ohos
TARGETS=$TARGETS,x86_64-unknown-linux-gnu

#export CC=clang
#export CXX=clang++
./configure --enable-local-rust --enable-profiler --disable-docs --set llvm.download-ci-llvm=false --set build.extended=true --set rust.rpath=true --set rust.lld=true --set rust.use-lld=true
python3 ./x.py dist #--host='' --target $TARGETS
python3 ./x.py dist --host='' --target $TARGETS

#~/rust-1.68.2/bin/rustc --target loongarch64-linux-ohos -C linker=../ohos/loongarch64-unknown-linux-ohos-clang.sh ~/helloworld.rs
